﻿define(function (require, exports, module) {

    var _bmForamt =
        {
            bookmarks: "bmks",
            parentIndex: "parent",
            curIndex: "cur",
            childs: "childs",
            title: "ttl",
            destInfo: "dest"
        };


    function FXPDFBookmarkInfo(nParentID, nCurID, szTitle, DestInfo) {
        this._nParentID = nParentID;
        this._nCurID = nCurID;
        this._DestInfo = DestInfo;
        this._szTitle = szTitle;
    }


    var FXPDFBookmark = function (bookmarkInfo) {
        this._BookmarkInfo = bookmarkInfo;
        this._childNodeList = [];
        this.AddChildBookmark = function (childNode) { this._childNodeList.push(childNode); };
        this.GetChildCount = function () { return this._childNodeList.length; };
        this.GetChild = function (index) { return this._childNodeList[index]; };
        this.GetBookmarkInfo = function () { return this._BookmarkInfo; };
    };


    FXPDFBookmark.prototype.parserBookmarkInfo = function (jsBookmarkInfo) {
        var Bookmarks = jsBookmarkInfo[_bmForamt.bookmarks];
        if (Bookmarks == null)
            return;
        for (var i = 0; i < Bookmarks.length; i++) {
            if (Bookmarks[i][_bmForamt.parentIndex] == 0) {
                var bookmarkInfoJs = Bookmarks[i];
                var node = this._AddBookmarkNode(this, bookmarkInfoJs, i);
                if (bookmarkInfoJs[_bmForamt.childs]) {
                    var childList = bookmarkInfoJs[_bmForamt.childs];
                    var nSize = childList.length;
                    for (var k = 0; k < nSize; k++) {
                        var index = childList[k] - 1;
                        var childNode = this._AddBookmarkNode(node, Bookmarks[index], index + 1);
                        this._EnumBookmark(childNode, index, Bookmarks);
                    }
                }
            }
        }
    };
    

    FXPDFBookmark.prototype._EnumBookmark = function (parentNode, index, Bookmarks) {
        var bookmarkInfoJs = Bookmarks[index];
        if (bookmarkInfoJs[_bmForamt.childs]) {
            var childList = bookmarkInfoJs[_bmForamt.childs];
            var nSize = childList.length;
            for (var k = 0; k < nSize; k++) {
                var index = childList[k] - 1;
                var childNode = this._AddBookmarkNode(parentNode, Bookmarks[index], index + 1);
                this._EnumBookmark(childNode, index, Bookmarks);
            }
        }
    };

    FXPDFBookmark.prototype._AddBookmarkNode = function (parentNode, BookmarksJs, index) {
        var destInfo = new FXREADER.PDFData.CPDF_DestInfo(BookmarksJs[_bmForamt.destInfo]);
        var BookmarkInfo = new FXPDFBookmarkInfo(BookmarksJs[_bmForamt.parentIndex], BookmarksJs[_bmForamt.curIndex],
                                            BookmarksJs[_bmForamt.title], destInfo);
        var bookmarkNode = new FXPDFBookmark(BookmarkInfo);
        parentNode.AddChildBookmark(bookmarkNode);
        return bookmarkNode;
    };


    FXREADER.PDFData.CPDF_BookMark = FXPDFBookmark;
    return FXREADER.PDFData.CPDF_BookMark;
});